﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PMS.BLL;
using PMS.ENT;
using PMS.Utilities;
using Ionic.Zip;
using System.IO;
using PMS.Hubs;

namespace PMS.Controllers
{
    public class ProjectController : Controller
    {
        //
        // GET: /Project/
        ChatHub chub = new ChatHub();
        public ActionResult Index()
        {
            return View();
        }

        #region Manage Project

        #region List
        public ActionResult ManageProject()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                #endregion

                #region LoadData
                int page = MvcApplication.pageDefault;
                int pageSize = MvcApplication.pageSizeDefault;
                int totalRecords = 0;
                int userID = 0;
                if (Utility.Role == Role.Teacher.ToString())
                {
                    userID = Utility.UserID;
                }
                ProjectCollection listProjet = ProjectBLL.GetProjectList(string.Empty, 0, userID, 0, 0, string.Empty, ProjectColumns.ProjectName.ToString(), OrderDirection.ASC.ToString(), page, pageSize, out totalRecords);

                LoadDataForList(0, 0, userID, 0);
                #endregion

                #region Pagination and search
                ViewData["ProjectName"] = string.Empty;
                ViewData["IsSearch"] = 0;
                ViewData["OrderBy"] = ProjectColumns.ProjectName.ToString();
                ViewData["OrderDirection"] = OrderDirection.ASC.ToString();
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listProjet);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult ManageProject(int[] projectID, string action, int isSearch, string projectName, int subjectID, int departmentID, int semesterID, int userID, string status, int? groupID, string orderBy, string orderDirection, int page, int pageSize, string url)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                #endregion

                #region Do Action
                if (action == ActionName.Delete.ToString())
                {
                    if (ProjectBLL.CheckIsOwn(projectID, Utility.UserID))
                    {
                        if (ProjectBLL.DeleteProject(Utility.StringParse(projectID), Utility.UserID) > 0)
                        {
                            TempData["Message"] = "Success, delete!";
                        }
                    }
                    else
                    {
                        TempData["Message"] = "Error, cannot delete projects are not yours!";
                    }
                }
                else if (action == ActionName.Confirm.ToString())
                {
                    if (ProjectBLL.CheckIsOwn(projectID, Utility.UserID))
                    {
                        if (ProjectBLL.ConfirmProject(Utility.StringParse(projectID), Utility.UserID) > 0)
                        {
                            TempData["Message"] = "Success, confirm!";
                        }
                    }
                    else
                    {
                        TempData["Message"] = "Error, cannot confirm projects are not yours!";
                    }
                }
                else if (action == ActionName.Join.ToString())
                {
                    int[] gID = { groupID ?? 0 };
                    if (ProjectBLL.CheckProjectStatus(projectID, ProjectStatus.Confirm.ToString()) && GroupBLL.CheckGroupStatus(gID, ProjectStatus.Confirm.ToString()))
                    {
                        int result = ProjectBLL.JoinProject(projectID[0], groupID ?? 0, Utility.UserID, Utility.semesterID);
                        if (result > 0)
                        {
                            TempData["Message"] = "Success, join project!";
                            //Notification
                            foreach (var item in NotificationBLL.InsertNotification(Utility.UserID, NotificationType.JoinProject, Utility.Role, groupID ?? 0, "/Project/ViewProject/" + projectID[0], projectID[0], 0))
                            {
                                chub.SendNotification(item.Description, item.Link, item.UserID.ToString());
                            }

                        }
                        else if (result == -1)
                        {
                            TempData["Message"] = "Error, already joined!";
                        }
                        else if (result == -2)
                        {
                            TempData["Message"] = "Error, project is full!";
                        }
                        else
                        {
                            TempData["Message"] = "Error, join project!";
                        }
                    }
                    else
                    {
                        TempData["Message"] = "Error, project is not confirm or group is not confirm!";
                    }
                }
                else if (action == ActionName.UnJoin.ToString())
                {

                    groupID = ProjectBLL.CheckIsJoin(projectID[0], Utility.UserID, Utility.semesterID);
                    int[] gID = { groupID ?? 0 };
                    if (groupID > 0)
                    {
                        ScheduleENT schedule = ScheduleBLL.CheckIsSchedule(projectID[0], groupID.Value, semesterID);
                        if (schedule.ScheduleID <= 0)
                        {
                            if (GroupBLL.CheckIsOwn(gID, Utility.UserID))
                            {
                                int result = ProjectBLL.UnJoinProject(projectID[0], groupID ?? 0, Utility.UserID, Utility.semesterID);
                                if (result > 0)
                                {
                                    TempData["Message"] = "Success, unjoin project!";
                                }
                                else
                                {
                                    TempData["Message"] = "Error, unjoin project!";
                                }
                            }
                            else
                            {
                                TempData["Message"] = "Error, you are not team leader!";
                            }
                        }
                        else
                        {
                            TempData["Message"] = "Error, have a schedule!";
                        }

                    }
                    else
                    {
                        TempData["Message"] = "Error, not participate in this project!";
                    }
                }
                #endregion

                #region LoadData
                int totalRecords = 0;

                ProjectCollection listProjet = ProjectBLL.GetProjectList(projectName, 0, userID, departmentID, subjectID, status, orderBy, orderDirection, page, pageSize, out totalRecords);

                LoadDataForList(subjectID, departmentID, userID, semesterID);
                #endregion

                #region Pagination
                ViewData["ProjectName"] = projectName;
                ViewData["IsSearch"] = isSearch;
                ViewData["OrderBy"] = orderBy;
                ViewData["OrderDirection"] = orderDirection;
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listProjet);
            }
            catch (DataAccessException ex)
            {
                TempData[PageInfo.Message.ToString()] = ex.Message;
                return RedirectToAction("Error", "Home"); ;
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }


        #endregion

        #region Active
        public ActionResult ActiveProject()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                #endregion

                #region LoadData
                int page = MvcApplication.pageDefault;
                int pageSize = MvcApplication.pageSizeDefault;
                int totalRecords = 0;
                int userID = 0;
                if (Utility.Role == Role.Teacher.ToString())
                {
                    userID = Utility.UserID;
                }
                ProjectCollection listProjet = ProjectBLL.GetProjectList(string.Empty, Utility.semesterID, userID, 0, 0, string.Empty, ProjectColumns.ProjectName.ToString(), OrderDirection.ASC.ToString(), page, pageSize, out totalRecords);

                LoadDataForList(0, 0, userID, Utility.semesterID);
                #endregion

                #region Pagination and search
                ViewData["ProjectName"] = string.Empty;
                ViewData["IsSearch"] = 0;
                ViewData["OrderBy"] = ProjectColumns.ProjectName.ToString();
                ViewData["OrderDirection"] = OrderDirection.ASC.ToString();
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listProjet);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult ActiveProject(int[] projectID, string action, int isSearch, string projectName, int subjectID, int departmentID, int userID, string status, int? groupID, string orderBy, string orderDirection, int page, int pageSize, string url)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                #endregion

                #region Do Action
                if (action == ActionName.Delete.ToString())
                {
                    if (ProjectBLL.CheckIsOwn(projectID, Utility.UserID))
                    {
                        if (ProjectBLL.DeleteProject(Utility.StringParse(projectID), Utility.UserID) > 0)
                        {
                            TempData["Message"] = "Success, delete!";
                        }
                    }
                    else
                    {
                        TempData["Message"] = "Error, cannot delete projects are not yours!";
                    }
                }
                else if (action == ActionName.Confirm.ToString())
                {
                    if (ProjectBLL.CheckIsOwn(projectID, Utility.UserID))
                    {
                        if (ProjectBLL.ConfirmProject(Utility.StringParse(projectID), Utility.UserID) > 0)
                        {
                            TempData["Message"] = "Success, confirm!";
                        }
                    }
                    else
                    {
                        TempData["Message"] = "Error, cannot confirm projects are not yours!";
                    }
                }
                else if (action == ActionName.Join.ToString())
                {
                    int[] gID = { groupID ?? 0 };
                    if (ProjectBLL.CheckProjectStatus(projectID, ProjectStatus.Confirm.ToString()) && GroupBLL.CheckGroupStatus(gID, ProjectStatus.Confirm.ToString()))
                    {
                        int result = ProjectBLL.JoinProject(projectID[0], groupID ?? 0, Utility.UserID, Utility.semesterID);
                        if (result > 0)
                        {
                            TempData["Message"] = "Success, join project!";
                            //Notification
                            foreach (var item in  NotificationBLL.InsertNotification(Utility.UserID, NotificationType.JoinProject, Utility.Role, groupID ?? 0, "/Project/ViewProject/" + projectID[0], projectID[0], 0))
                            {
                                chub.SendNotification(item.Description, item.Link, item.UserID.ToString());
                            }
                           

                        }
                        else if (result == -1)
                        {
                            TempData["Message"] = "Error, already joined!";
                        }
                        else if (result == -2)
                        {
                            TempData["Message"] = "Error, project is full!";
                        }
                        else
                        {
                            TempData["Message"] = "Error, join project!";
                        }
                    }
                    else
                    {
                        TempData["Message"] = "Error, project is not confirm or group is not confirm!";
                    }
                }
                else if (action == ActionName.UnJoin.ToString())
                {

                    groupID = ProjectBLL.CheckIsJoin(projectID[0], Utility.UserID, Utility.semesterID);
                    int[] gID = { groupID ?? 0 };
                    if (groupID > 0)
                    {
                        ScheduleENT schedule = ScheduleBLL.CheckIsSchedule(projectID[0], groupID.Value, Utility.semesterID);
                        if (schedule.ScheduleID <= 0)
                        {
                            if (GroupBLL.CheckIsOwn(gID, Utility.UserID))
                            {
                                int result = ProjectBLL.UnJoinProject(projectID[0], groupID ?? 0, Utility.UserID, Utility.semesterID);
                                if (result > 0)
                                {
                                    TempData["Message"] = "Success, unjoin project!";
                                }
                                else
                                {
                                    TempData["Message"] = "Error, unjoin project!";
                                }
                            }
                            else
                            {
                                TempData["Message"] = "Error, you are not team leader!";
                            }
                        }
                        else
                        {
                            TempData["Message"] = "Error, have a schedule!";
                        }

                    }
                    else
                    {
                        TempData["Message"] = "Error, not participate in this project!";
                    }
                }
                #endregion

                #region LoadData
                int totalRecords = 0;

                ProjectCollection listProjet = ProjectBLL.GetProjectList(projectName, Utility.semesterID, userID, departmentID, subjectID, status, orderBy, orderDirection, page, pageSize, out totalRecords);

                LoadDataForList(subjectID, departmentID, userID, Utility.semesterID);
                #endregion

                #region Pagination
                ViewData["ProjectName"] = projectName;
                ViewData["IsSearch"] = isSearch;
                ViewData["OrderBy"] = orderBy;
                ViewData["OrderDirection"] = orderDirection;
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listProjet);
            }
            catch (DataAccessException ex)
            {
                TempData[PageInfo.Message.ToString()] = ex.Message;
                return RedirectToAction("Error", "Home"); ;
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }


        #endregion

        #region Add Project
        public ActionResult AddProject()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 2)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                ProjectENT project = new ProjectENT
                {

                    Status = ProjectStatus.Confirm.ToString()
                };

                LoadDataForDetail(project.SubjectID, project.DepartmentID);

                ViewData["Action"] = ActionName.Insert.ToString();
                #endregion

                return View("Project", project);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult AddProject(ProjectENT project)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 2)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                project.StartDate = DateTime.Today;
                project.EndDate = DateTime.Today;
                project.OwnerID = Utility.UserID;
                project.CreatedBy = Utility.UserID;
                project.UpdateBy = Utility.UserID;

                int result = ProjectBLL.InsertProject(project);
                if (result > 0)
                {
                    TempData["Message"] = "Success, insert project!";
                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error, Project name exists!";

                }

                #endregion

                #region LoadData

                LoadDataForDetail(project.SubjectID, project.DepartmentID);


                #endregion

                return RedirectToAction("EditProject", new { id = result });
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region Edit Project
        public ActionResult EditProject(int ID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 2)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                int[] projectID = { ID };

                if (!ProjectBLL.CheckIsOwn(projectID, Utility.UserID))
                {
                    TempData["Message"] = "Cannot edit project is not yours!";
                    return RedirectToAction("ManageProject");
                }

                ProjectENT project = ProjectBLL.GetProjectByID(ID);

                LoadDataForDetail(project.SubjectID, project.DepartmentID);

                // Kiet Thêm project ID Để Update
                ViewData["ProjectID"] = project.ProjectID;
                ViewData["Action"] = ActionName.Update.ToString();
                #endregion

                return View("Project", project);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult EditProject(int ID, ProjectENT project)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 2)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                project.UpdateBy = Utility.UserID;

                int result = ProjectBLL.UpdateProject(project);
                if (result > 0)
                {
                    TempData["Message"] = "Success, Update project!";
                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error, Project name exists!";
                }

                #endregion

                #region LoadData

                LoadDataForDetail(project.SubjectID, project.DepartmentID);

                ViewData["Action"] = ActionName.Update.ToString();

                #endregion

                return View("Project", project);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region View Project
        public ActionResult ViewProject(int ID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                #endregion

                #region LoadData

                int[] projectID = { ID };


                ProjectENT project = ProjectBLL.GetProjectByID(ID);
                int page = MvcApplication.pageDefault;
                int pageSize = MvcApplication.pageSizeDefault;
                int totalRecords = 0;
                GroupCollection listGroup = GroupBLL.GetGroupList(string.Empty, Utility.semesterID, ID, 0, 0, string.Empty, GroupColumns.GroupName.ToString(), OrderDirection.ASC.ToString(), page, pageSize, out totalRecords);

                ViewData["OrderBy"] = GroupColumns.GroupName.ToString();
                ViewData["OrderDirection"] = OrderDirection.ASC.ToString();
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;
                LoadDataForDetail(project.SubjectID, project.DepartmentID);

                ViewData["GroupList"] = listGroup;
                ViewData["ProjectID"] = ID;
                ViewData["SemesterID"] = Utility.semesterID;
                ViewData["Permission"] = project.OwnerID == Utility.UserID ? true : false;

                ViewData["Action"] = ActionName.View.ToString();
                #endregion

                return View("Project", project);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult ViewProject(int ID, string action, int scheduleID, int groupID, string orderBy, string orderDirection, int page, int pageSize, string url)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                #endregion

                #region DoAction
                if (action == ActionName.Schedule.ToString())
                {
                    int[] gID = { groupID };
                    int[] sID = { scheduleID };
                    if (ScheduleBLL.CheckScheduleStatus(sID, ProjectStatus.Confirm.ToString()) && GroupBLL.CheckGroupStatus(gID, GroupStatus.Confirm.ToString()))
                    {
                        int result = ScheduleBLL.JoinSchedule(scheduleID, groupID, Utility.UserID);
                        if (result > 0)
                        {
                            TempData["Message"] = "Success, add schedule to group!";
                            //Notification
                            foreach (var item in NotificationBLL.InsertNotification(Utility.UserID, NotificationType.AddSchedule, Utility.Role, groupID, "/Schedule/Upload/" + result, result, 0))
                            {
                                chub.SendNotification(item.Description, item.Link, item.UserID.ToString());
                            }
                            

                        }
                        else if (result == -1)
                        {
                            TempData["Message"] = "Error, already added!";
                        }
                        else
                        {
                            TempData["Message"] = "Error, add schedule to group!";
                        }
                    }
                    else
                    {
                        TempData["Message"] = "Error, schedule or group is not confirm!";
                    }
                }
                #endregion

                #region LoadData

                int[] projectID = { ID };


                ProjectENT project = ProjectBLL.GetProjectByID(ID);
                int totalRecords = 0;
                GroupCollection listGroup = GroupBLL.GetGroupList(string.Empty, Utility.semesterID, ID, 0, 0, string.Empty, orderBy, orderDirection, page, pageSize, out totalRecords);

                ViewData["OrderBy"] = orderBy;
                ViewData["OrderDirection"] = orderDirection;
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;
                LoadDataForDetail(project.SubjectID, project.DepartmentID);

                ViewData["GroupList"] = listGroup;
                ViewData["ProjectID"] = ID;
                ViewData["SemesterID"] = Utility.semesterID;

                ViewData["Action"] = ActionName.View.ToString();
                #endregion



                return View("Project", project);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region LoadData
        public void LoadDataForList(int subjectID, int departmentID, int userID, int semesterID)
        {
            ViewData["SubjectList"] = new SelectList(SubjectBLL.GetSubjectListForCMB(0), SubjectColumns.SubjectID.ToString(), SubjectColumns.SubjectName.ToString(), subjectID);
            ViewData["UserList"] = new SelectList(UserBLL.GetUserListForCMB(2), UserColumns.UserID.ToString(), UserColumns.FullName.ToString(), userID);
            ViewData["DepartmentList"] = new SelectList(DepartmentBLL.GetDepartmentListForCMB(), DepartmentColumns.DepartmentID.ToString(), DepartmentColumns.DepartmentName.ToString(), departmentID);
            ViewData["SemesterList"] = new SelectList(SemesterBLL.GetSemesterListForCMB(), SemesterColumns.SemesterID.ToString(), SemesterColumns.SemesterName.ToString(), semesterID);

        }

        public void LoadDataForDetail(int subjectID, int departmentID)
        {
            ViewData["SubjectList"] = new SelectList(SubjectBLL.GetSubjectListForCMB(departmentID), SubjectColumns.SubjectID.ToString(), SubjectColumns.SubjectName.ToString(), subjectID);
            ViewData["DepartmentList"] = new SelectList(DepartmentBLL.GetDepartmentListForCMB(), DepartmentColumns.DepartmentID.ToString(), DepartmentColumns.DepartmentName.ToString(), departmentID);

        }

        #endregion

        #endregion

        #region Manage Group

        #region List
        public ActionResult ManageGroup(int? scheduleID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                #endregion

                #region LoadData
                int page = MvcApplication.pageDefault;
                int pageSize = MvcApplication.pageSizeDefault;
                int totalRecords = 0;
                int userID = 0;
                if (Utility.Role == Role.Student.ToString())
                {
                    userID = Utility.UserID;
                }
                GroupCollection listGroup = GroupBLL.GetGroupList(string.Empty, Utility.semesterID, 0, scheduleID ?? 0, userID, string.Empty, GroupColumns.GroupName.ToString(), OrderDirection.ASC.ToString(), page, pageSize, out totalRecords);

                LoadDataForGroupList(Utility.semesterID, 0, scheduleID ?? 0, userID);
                #endregion

                #region Pagination and search
                ViewData["GroupName"] = string.Empty;
                ViewData["Status"] = "--All--";
                ViewData["IsSearch"] = 0;
                ViewData["OrderBy"] = GroupColumns.GroupName.ToString();
                ViewData["OrderDirection"] = OrderDirection.ASC.ToString();
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listGroup);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult ManageGroup(int[] groupID, string action, int isSearch, string groupName, int semesterID, int projectID, int scheduleID, int? joinScheduleID, int userID, string status, string orderBy, string orderDirection, int page, int pageSize, string url)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                #endregion

                #region Do Action
                if (action == ActionName.Delete.ToString())
                {
                    if (GroupBLL.CheckIsOwnAndDraft(groupID, Utility.UserID))
                    {
                        if (GroupBLL.DeleteGroup(Utility.StringParse(groupID), Utility.UserID) > 0)
                        {
                            TempData["Message"] = "Success, delete!";
                        }
                    }
                    else
                    {
                        TempData["Message"] = "Error, cannot delete groups are not yours or in progress!";
                    }
                }
                else if (action == ActionName.Confirm.ToString())
                {
                    if (GroupBLL.CheckIsOwnAndDraft(groupID, Utility.UserID))
                    {
                        if (GroupBLL.ConfirmGroup(Utility.StringParse(groupID), Utility.UserID) > 0)
                        {
                            TempData["Message"] = "Success, confirm!";
                            foreach (var item in NotificationBLL.InsertNotification(Utility.UserID, NotificationType.ConfirmGroup, Utility.Role, Utility.IntParse(groupID), "/Project/ViewGroup/" + groupID, 0, 0))
                            {
                                chub.SendNotification(item.Description, item.Link, item.UserID.ToString());
                            }
                            
                        }
                    }
                    else
                    {
                        TempData["Message"] = "Error, cannot confirm groups are not yours or already confirm!";
                    }
                }
                else if (action == ActionName.Schedule.ToString())
                {
                    if (GroupBLL.CheckIsOwnAndDraft(groupID, Utility.UserID))
                    {
                        if (GroupBLL.ConfirmGroup(Utility.StringParse(groupID), Utility.UserID) > 0)
                        {
                            TempData["Message"] = "Success, add schedule!";
                        }
                    }
                    else
                    {
                        TempData["Message"] = "Error, cannot add schedule to group are not yours or already confirm!";
                    }
                }

                #endregion

                #region LoadData
                string sstatus = string.Empty;
                if (status == "--All--")
                {
                    sstatus = string.Empty;
                }
                else
                {
                    sstatus = status;
                }
                int totalRecords = 0;

                GroupCollection listProjet = GroupBLL.GetGroupList(groupName, semesterID, projectID, scheduleID, userID, sstatus, orderBy, orderDirection, page, pageSize, out totalRecords);

                LoadDataForGroupList(semesterID, projectID, scheduleID, userID);
                #endregion

                #region Pagination
                ViewData["GroupName"] = groupName;
                ViewData["Status"] = status;
                ViewData["IsSearch"] = isSearch;
                ViewData["OrderBy"] = orderBy;
                ViewData["OrderDirection"] = orderDirection;
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listProjet);
            }
            catch (DataAccessException ex)
            {
                TempData[PageInfo.Message.ToString()] = ex.Message;
                return RedirectToAction("Error", "Home"); ;
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }


        #endregion

        #region Add

        public ActionResult AddGroup()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 3)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                GroupENT group = new GroupENT
                {
                    OwnerID = Utility.UserID,
                    Status = GroupStatus.Draft.ToString(),
                };
                group.ListGroupMember = new UserGroupCollection();

                ViewData["Action"] = ActionName.Insert.ToString();

                LoadDataForGroupAdd(Utility.UserID);
                #endregion

                return View("Group", group);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult AddGroup(GroupENT group, string url)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 3)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                group.CreatedBy = Utility.UserID;
                group.UpdateBy = Utility.UserID;
                group.Status = GroupStatus.Draft.ToString();

                int result = GroupBLL.InsertGroup(group);
                if (result > 0)
                {
                    TempData["Message"] = "Success, insert group!";

                    //Notification
                    foreach (var item in NotificationBLL.InsertNotification(Utility.UserID, NotificationType.InviteGroup, Utility.Role, result, "/Project/ViewGroup/" + result, 0, 0))
                    {
                        chub.SendNotification(item.Description, item.Link, item.UserID.ToString());
                    }
                    

                    return RedirectToAction("EditGroup", new { id = result });
                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error, Group name exists!";
                }
                else if (result == -2)
                {
                    TempData["Message"] = "Error, insert member!";
                }
                else if (result == -3)
                {
                    TempData["Message"] = "Error, update member!";
                }

                #endregion

                #region LoadData



                #endregion
                return RedirectToAction("AddGroup");

            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region Edit Group
        public ActionResult EditGroup(int ID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 3)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                int[] groupID = { ID };



                if (!GroupBLL.CheckIsOwnAndDraft(groupID, Utility.UserID))
                {
                    TempData["Message"] = "Cannot edit group is not yours or draft!";
                    return RedirectToAction("ManageGroup");
                }

                GroupENT group = GroupBLL.GetGroupByID(ID);
                group.ListGroupMember = UserGroupBLL.GetUserGroupListByGroupID(ID);

                string userID = string.Empty;
                for (int i = 0; i < group.ListGroupMember.Count; i++)
                {
                    userID += group.ListGroupMember[i].UserID + ",";
                }

                if (userID.EndsWith(","))
                {
                    userID = userID.Remove(userID.Length - 1);
                }

                LoadDataForGroupEdit(group.ListGroupMember.Count, userID);

                ViewData["Action"] = ActionName.Update.ToString();
                #endregion

                return View("Group", group);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult EditGroup(int ID, GroupENT group)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 3)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                group.UpdateBy = Utility.UserID;


                //send notification & set status 
                UserGroupCollection newuser = new UserGroupCollection();
                foreach (var useritem in group.ListGroupMember)
                {
                    if (useritem.UserGroupID <= 0)
                    {
                        useritem.Status = "Waiting for Confirm";
                        newuser.Add(useritem);
                    }
                }


                int result = GroupBLL.UpdateGroup(group);
                if (result > 0)
                {
                    TempData["Message"] = "Success, Update group!";
                    //Notification khi mời thêm thành viên
                    NotificationBLL.NewUser = newuser;
                    foreach (var item in NotificationBLL.InsertNotification(Utility.UserID, NotificationType.InviteGroupMore, Utility.Role, group.GroupID, "/Project/ViewGroup/" + group.GroupID, 0, 0))
                    {
                        chub.SendNotification(item.Description, item.Link, item.UserID.ToString());
                    }
                    

                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error, Group name exists!";
                }

                #endregion

                #region LoadData

                string userID = string.Empty;
                for (int i = 0; i < group.ListGroupMember.Count; i++)
                {
                    userID += group.ListGroupMember[i].UserID + ",";
                }

                if (userID.EndsWith(","))
                {
                    userID = userID.Remove(userID.Length - 1);
                }
                LoadDataForGroupEdit(group.ListGroupMember.Count, userID);
                ViewData["Action"] = ActionName.Update.ToString();

                #endregion

                return View("Group", group);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region View Group
        public ActionResult ViewGroup(int ID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                #endregion

                #region LoadData

                int[] groupID = { ID };



                GroupENT group = GroupBLL.GetGroupByID(ID);
                group.ListGroupMember = UserGroupBLL.GetUserGroupListByGroupID(ID);



                string userID = string.Empty;
                for (int i = 0; i < group.ListGroupMember.Count; i++)
                {
                    userID += group.ListGroupMember[i].UserID + ",";
                }

                if (userID.EndsWith(","))
                {
                    userID = userID.Remove(userID.Length - 1);
                }

                LoadDataForGroupEdit(group.ListGroupMember.Count, userID);

                ViewData["Action"] = ActionName.View.ToString();
                #endregion

                return View("Group", group);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        #endregion

        #region LoadData
        public void LoadDataForGroupList(int semesterID, int projectID, int scheduleID, int userID)
        {
            ViewData["ProjectList"] = new SelectList(ProjectBLL.GetProjectListForCMB(0), ProjectColumns.ProjectID.ToString(), ProjectColumns.ProjectName.ToString(), projectID);
            ViewData["ScheduleList"] = new SelectList(ScheduleBLL.GetScheduleListForCMB(0, projectID, semesterID), ScheduleColumns.ScheduleID.ToString(), ScheduleColumns.ScheduleName.ToString(), scheduleID);
            ViewData["UserList"] = new SelectList(UserBLL.GetUserListForCMB(3), UserColumns.UserID.ToString(), UserColumns.FullName.ToString(), userID);
            ViewData["SemesterList"] = new SelectList(SemesterBLL.GetSemesterListForCMB(), SemesterColumns.SemesterID.ToString(), SemesterColumns.SemesterName.ToString(), semesterID);

        }

        public void LoadDataForGroupEdit(int count, string userID)
        {
            string[] split = userID.Split(',');
            UserCollection listUser = UserBLL.GetUserListForCMB(3);
            for (int i = 0; i < count; i++)
            {
                ViewData["UserList" + i] = new SelectList(listUser, UserColumns.UserID.ToString(), UserColumns.FullName.ToString(), Utility.IntParse(split[i]));
            }
            ViewData["UserList"] = new SelectList(UserBLL.GetUserListForCMB(3), UserColumns.UserID.ToString(), UserColumns.FullName.ToString());

        }

        public void LoadDataForGroupAdd(int userID)
        {

            ViewData["UserList"] = new SelectList(UserBLL.GetUserListForCMB(3), UserColumns.UserID.ToString(), UserColumns.FullName.ToString(), userID);

        }
        #endregion

        #endregion

        #region Reference
        //public ActionResult DownloadAllFile(int projectID, int groupID, int semesterID, int departmentID, int subjectID)
        //{

        //    try
        //    {
        //        ZipFile zip = new ZipFile();
        //        string path = "c:\\TempCSV";
        //        string filename = Path.Combine(path,"AllFile.zip");
        //        if (Directory.Exists(path))
        //        {
        //            zip.AddDirectory(path);
        //            zip.Save(filename);
        //        }
        //        else
        //        {
        //            TempData["Message"] = "Error, No content found!";
        //            return RedirectToAction("Reference");
        //        }
        //        FileInfo file = new FileInfo(filename);

        //        Stream stream = file. 
        //        Response.AddHeader("content-dispostion", "attachemnt; filename=Projectreport.doc");
        //        Response.AddHeader("content-type", "application/msword");
        //        stream.Seek(0, System.IO.SeekOrigin.Begin);

        //        StreamReader reader = new StreamReader(stream);
        //        byte[] fileData = new byte[stream.Length];
        //        stream.Read(fileData, 0, (int)stream.Length);

        //        Response.AddHeader("Content-Length", stream.Length.ToString());
        //        Response.BinaryWrite(fileData);
        //        stream.Close();
        //        return null;
        //    }
        //    catch (DataAccessException)
        //    {
        //        TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
        //        return RedirectToAction("Error", "Home");
        //    }
        //    catch (Exception ex)
        //    {
        //        LogBLL.LogEx("BLM_ERR_Common", ex);
        //        TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
        //        return RedirectToAction("Error", "Home");
        //    }
        //}
        #endregion

        #region Ajax
        public JsonResult GetSubjectByDepartmentID(int? ID)
        {
            try
            {
                SubjectCollection listSubject = SubjectBLL.GetSubjectListForCMB(ID ?? 0);
                return Json(new { ListSubject = listSubject }, JsonRequestBehavior.DenyGet);

            }
            catch
            {
                return null;
            }
        }

        public JsonResult GetProjectBySubjectID(int? ID)
        {
            try
            {
                ProjectCollection listProject = ProjectBLL.GetProjectListBySubjectID(ID ?? 0);
                return Json(new { ListProject = listProject }, JsonRequestBehavior.DenyGet);

            }
            catch
            {
                return null;
            }
        }

        public JsonResult GetGroupByProjectID(int? ID)
        {
            try
            {
                GroupCollection listGroup = GroupBLL.GetGroupListForCMB(Utility.UserID, ID ?? 0);
                return Json(new { ListGroup = listGroup }, JsonRequestBehavior.DenyGet);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public JsonResult GetProject(int? ID)
        {
            try
            {
                ProjectENT project = ProjectBLL.GetProjectByID(ID ?? 0);
                string startDate = project.StartDate.ToString("yyyy/MM/dd");
                string endDate = project.EndDate.ToString("yyyy/MM/dd");
                return Json(new { StartDate = startDate, EndDate = endDate }, JsonRequestBehavior.DenyGet);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public JsonResult CheckUserInGroup(int? groupID)
        {
            try
            {
                bool result = true;

                UserCollection listuser = UserBLL.GetUserByIDAndGroupID(Utility.UserID, groupID ?? 0);
                if (listuser.Count <= 0)
                {
                    result = false;
                }

                return Json(result, JsonRequestBehavior.DenyGet);
            }
            catch (Exception)
            {

                return Json(null, JsonRequestBehavior.DenyGet);
            }
        }

        public JsonResult CheckUserGroupPermission(int? groupID)
        {
            try
            {
                UserGroupCollection listusergroup = UserGroupBLL.GetUserGroupListByGroupID(groupID ?? 0);
                UserGroupENT usergroup = listusergroup.Where(d => d.UserID == Utility.UserID).First();
                if (usergroup != null)
                {
                    return Json(new { perdeletefile = usergroup.IsDeleteFile, peruploadfile = usergroup.IsUploadFile }, JsonRequestBehavior.DenyGet);
                }
                return Json(null, JsonRequestBehavior.DenyGet);
            }
            catch (Exception)
            {

                return Json(null, JsonRequestBehavior.DenyGet);
            }

        }

        public JsonResult CheckIsFull(int? projectID)
        {
            try
            {
                bool result = false;

                ProjectENT project = ProjectBLL.GetProjectBySemesterID(projectID ?? 0, Utility.semesterID);
                if (project.NumberOfGroup <= project.NGroup)
                {
                    result = true;
                }

                return Json(result, JsonRequestBehavior.DenyGet);
            }
            catch (Exception)
            {

                return Json(null, JsonRequestBehavior.DenyGet);
            }
        }

        public JsonResult GetJoinGroupByProjectID(int projectID, int semesterID)
        {
            try
            {
                GroupCollection listGroup = GroupBLL.GetJoinGroupList(projectID, semesterID);
                return Json(new { ListGroup = listGroup }, JsonRequestBehavior.DenyGet);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public JsonResult GetAcceptGroupByProjectID(int projectID, int semesterID)
        {
            try
            {
                GroupCollection listGroup = GroupBLL.GetAcceptGroupList(projectID, semesterID);
                return Json(new { ListGroup = listGroup }, JsonRequestBehavior.DenyGet);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public JsonResult GetProjectForCMB()
        {
            try
            {
                ProjectCollection listProject = ProjectBLL.GetProjectListForCMB(0);
                return Json(new { ListProject = listProject }, JsonRequestBehavior.DenyGet);

            }
            catch
            {
                return null;
            }
        }

        public JsonResult ConfirmUserGroup(int? usergroupID, string status)
        {
            try
            {
                UserGroupBLL.ConfirmGroup(usergroupID.ToString(), Utility.UserID, status);
                return Json(new { Result = true, Message = "Sucess Confirm Group!" }, JsonRequestBehavior.DenyGet);
            }
            catch (Exception)
            {

                return Json(new { Result = false, Message = "Error Confirm Group!" }, JsonRequestBehavior.DenyGet);
            }
        }

        public JsonResult DeleteUserGroup(int? usergroupID)
        {
            try
            {
                UserGroupBLL.DeleteUserGroup(usergroupID.ToString(), Utility.UserID);
                return Json(new { Result = true, Message = "Sucess Delete User Group!" }, JsonRequestBehavior.DenyGet);
            }
            catch (Exception)
            {

                return Json(new { Result = false, Message = "Error Delete User Group!" }, JsonRequestBehavior.DenyGet);
            }
        }

        #endregion

    }
}
